import indexHandler from "/handlers/indexHandler.js"
$(document).ready(function () {
    Qmsg.config({
        showClose: false,
        timeout: 1500
    })
    $('.n-con-left-switcher').click(function () {
        let lgdis = $('.login').css('display');
        let rgdis = $('.regis').css('display');
        if (lgdis == 'block' && rgdis == 'none') {
            $('.login').css("display", "none");
            $('.regis').css("display", "block");
            $('.p2').css("display", "inline-block");
            $('.nick').css("display", "inline-block");
            $('.n-con-left-switcher')[0].innerHTML = '已有帐号？去登录！';
        } else {
            $('.login').css("display", "block");
            $('.regis').css("display", "none");
            $('.p2').css("display", "none");
            $('.nick').css("display", "none");
            $('.n-con-left-switcher')[0].innerHTML = '没有账号？立即注册！';
        }
    })
    $('.login').click(async function () {
        let a = $('.a').val();
        let p = $('.p1').val();
        var loadingMsg = Qmsg.loading('登录中', {
            autoClose: false
        });
        let rsl = await indexHandler.loginHandler(a, p);
        switch (rsl) {
            case 1:
                loadingMsg.close();
                Qmsg.success('登录成功！');
                window.setTimeout(function () {
                    window.location.reload();
                }, 1800)
                break;
            case 2:
                loadingMsg.close();
                Qmsg.error('账号或密码错误，请重试！');
                break;
            case 3:
                loadingMsg.close();
                Qmsg.error('发送登录请求失败，请重试！');
                break;
            case 4:
                loadingMsg.close();
                Qmsg.error('用户账号不能为空！');
                break;
            case 5:
                loadingMsg.close();
                Qmsg.error('用户密码不能为空！');
                break;
        }
    })

    $('.regis').click(async function () {
        let a = $('.a').val();
        let n = $('.nick').val();
        let p1 = $('.p1').val();
        let p2 = $('.p2').val();
        var loadingMsg = Qmsg.loading('注册中', {
            autoClose: false
        });
        let rsl = await indexHandler.registerHandler(a, n, p1, p2);
        switch (rsl) {
            case 0:
                loadingMsg.close();
                Qmsg.error('注册失败，请重试！');
                break;
            case 1:
                loadingMsg.close();
                Qmsg.success('注册成功！');
                window.setTimeout(function () {
                    window.location.reload();
                }, 1800)
                break;
            case 2:
                loadingMsg.close();
                Qmsg.error('注册失败，账号已存在！');
                break;
            case 3:
                loadingMsg.close();
                Qmsg.error('发送注册请求失败，请重试！');
                break;
            case 4:
                loadingMsg.close();
                Qmsg.error('账号或昵称长度不能大于9位，请重试！');
                break;
            case 5:
                loadingMsg.close();
                Qmsg.error('两次输入密码不一致！');
                break;
            case 6:
                loadingMsg.close();
                Qmsg.error('某项内容为空，请检查！');
                break;
        }
    })
});